package org.xczy.boot.controller;

import org.springframework.web.bind.annotation.*;
import org.xczy.boot.entity.SysUser;
import org.xczy.boot.result.Result;
import org.xczy.boot.service.SysUserService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class SysUserController {

  @Resource
  private SysUserService sysUserService;

  @GetMapping("/{id}")
  public Result<SysUser> getUserById(@PathVariable("id") String id){
    SysUser user = sysUserService.getUserById(id);
    if(user != null){
      user.setPassword("******");
      return Result.ok(user);
    }
    return Result.fail(null);
  }

  @GetMapping("/all")
  public Result<List<SysUser>> getUserAll(){
    List<SysUser> users = sysUserService.getUserAll();
    if(users != null){
      for (SysUser user: users) {
        user.setPassword("******");
      }
      return Result.ok(users);
    }
    return Result.fail(null);
  }

  @PostMapping("/add")
  public Result<String> setUsers(@RequestBody SysUser sysUser){
    int i = sysUserService.setUsers(sysUser);
    if(i > 0) {
      return Result.ok("添加成功");
    }
    return Result.fail("添加失败");
  }

}
